/*
 * @Description:
 * @Author: 胡涛
 * @Date: 2023-06-06 09:20:37
 * @LastEditors: 胡涛
 * @LastEditTime: 2024-09-05 23:19:57
 */

import { ContextUser, JWTToken } from "@/api/services/types";
import { defineStore } from "pinia";
// import router from "@/router";
interface IAppState {
  access_token: string;
  token_type: string;
  expires_in: number;
  refresh_token: string;
  token_time: number;
  activeTab: string
  contextUser: ContextUser
  colorPrimary:string
  menuData: any[]
}

export const useAppStore = defineStore("app", {
  state: (): IAppState => {
    return {
      access_token: "",
      token_type: "",
      expires_in: 0,
      refresh_token: "",
      token_time: 0,
      activeTab: 'home',
      contextUser: {
        userId: "",
        userName: "",
        surname: "",
        phoneNumber: "",
        email: "",
        avatar: "",
        departmentName: "",
        roleNames: [],
        roleDescriptions: "",
        permissions: [],
        introduction: "",
        departmentId: null,
        name: ""
      },
      colorPrimary:'',
      menuData: []
    };
  },
  actions: {
 
    setToken(token: JWTToken) {
      this.access_token = token.access_token;
      this.token_type = token.token_type || 'Bearer';
      this.expires_in = token.expires_in;
      this.refresh_token = token.refresh_token;
      this.token_time = new Date().getTime();
    },


    setContextUser(context: ContextUser) {
      this.contextUser = context;
    },
    setMenuData(menuData: any[]) {
      this.menuData = menuData;
    },
    logout() {
      this.contextUser = {
          userId: "",
        userName: "",
        surname: "",
        phoneNumber: "",
        email: "",
        avatar: "",
        departmentName: "",
        roleNames: [],
        roleDescriptions: "",
        permissions: [],
        introduction: "",
        departmentId: null,
        name: ""
      }
      this.access_token='';
      this.token_type='';
      this.token_time=0;
      this.refresh_token='';
      this.activeTab='home';
    }
  },
  persist: {
    enabled: true,
    strategies: [{ storage: localStorage, key: "vite-vue-store-app" }],
  },
});
